﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Aspose.Cells;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminOrders : System.Web.UI.Page
    {

        private Int32 pageIndex = 1;
        private Int32 pageSize = 5;
        private Int32 totalRecord;
        private Int32 orderSate = HangSo.ORDER_STATE_APPENDING;
        private Cells _range;
        private Worksheet _exSheet;

        protected void Page_Load(object sender, EventArgs e)
        {            

            try
            {
                orderSate = Int32.Parse(Request.QueryString["OrderState"].ToString());
            }
            catch (Exception ex)
            {
                orderSate = HangSo.ORDER_STATE_APPENDING;
            }

            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch (Exception)
            {
                pageIndex = 1;
            }

            pageSize = this.PaggingControl1.PageSize;

            if (!Page.IsPostBack)
            {
                Int32 totalOrderAppending = 0;// tổng số các order đang đợi để ship
                Int32 totalOrderShipping = 0; // tổng số các order đang ship
                //lấy thông số các order
                OrderProvider.GetParametersOfOrder(out totalOrderAppending, out totalOrderShipping); 
                this.hlOrderAppending.Text = "Order Appending (" + totalOrderAppending.ToString() + ")";
                this.hlOrderShipping.Text = "Order Shipping (" + totalOrderShipping.ToString() + ")";
                //load dữ liệu order theo trạng thái
                LoadDataOrder(orderSate);
            }
        }

        private void LoadDataOrder(Int32 orderState)
        {
            DataSet dsOrders = OrderProvider.GetOrdersByOrderState(orderState, pageIndex, pageSize, out totalRecord);
            this.rpOrders.DataSource = dsOrders;
            this.rpOrders.DataBind();

            this.PaggingControl1.PageURL = "AdminOrders.aspx?OrderState=" + orderState.ToString();
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PagingSize = pageSize;
        }
     

        protected void btnexport_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet dsOrdersExport = OrderProvider.GetOrdersExportCVS();

                DataTable dtOrders = dsOrdersExport.Tables[0];
               
                // chuẩn bị tệp ghi dữ liêu
                string excelFile = Server.MapPath("~/ExcelTem/ListOrder.xls");
                Workbook exBook = new Workbook();
                exBook.Open(excelFile, FileFormatType.Excel2003);
                _exSheet = exBook.Worksheets[0];
                _range = _exSheet.Cells;
                // Ghi du lieu vao file excel
                int i = 5;
                FillData("LISTE ORDER", 2, 2);

                for (int j = 0; j < dtOrders.Rows.Count - 1; j++)
                {
                    FillData(dtOrders.Rows[j]["ReceivedName"], i, 1);
                    FillData(dtOrders.Rows[j]["ReceivedAddress"], i, 2);
                    FillData(dtOrders.Rows[j]["ReceivedCity"], i, 3);
                    FillData(dtOrders.Rows[j]["ReceicedPhone"], i, 4);                   
                    i++;
                }
                exBook.Save("ListOrder.xls", SaveType.OpenInExcel, FileFormatType.Default, this.Response);
            }

            catch (Exception ex)
            {
                Response.Redirect("AdminOrders.aspx");
            }
        }
        // ham fill du lieu vao excel
        protected void FillData(object value, int row, int column)
        {
            try
            {
                _exSheet.Cells[row - 1, column - 1].PutValue(value);
            }
            catch (Exception e)
            {

            }
        }
    }
}